﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Peradigm.Framework.UI.Web;

using Liberty.Application.ServiceMemberManagement;
using Liberty.Application.GrantManagement;
using Liberty.Domain.CaseManagement;
using Liberty.Domain.GrantManagement;
using Liberty.Domain.NoteManagement;

namespace Liberty.UI.GrantManagement
{
    public partial class GrantView : AbstractLibertyPage
    {
        #region Constants
        private const string REDIRECT_URL = "GrantView.RedirectUrl";
        #endregion

        #region Event Handlers
        protected void Page_Load( object sender, EventArgs e )
        {
            if ( !this.IsPostBack )
            {
                this.RedirectURL = this.Request.UrlReferrer.AbsoluteUri.ToString();
            }
            List<Note> notes = this.NotesFor( this.Grant );
            this.ucGrantDetailView.Model = this.Grant;
            this.ucGrantEditView.Model = this.Grant;
            this.ucNoteListView.Model = notes;
            this.ucNewNoteView.NoteAddedListeners += new NoteAdded( this.OnNoteAdded );
            this.ucAuditTrail.Model = this.Grant.Events;
        }
        protected void cmdSave_Click( object sender, EventArgs e )
        {
            this.ucGrantEditView.SaveModel();
            this.ToggleGrantView( false );
        }
        protected void cmdCancel_Click( object sender, EventArgs e )
        {
            if ( !String.IsNullOrEmpty( this.RedirectURL ) )
            {
                Response.Redirect( this.RedirectURL );
            }
        }
        protected void cmdEdit_Click( object sender, EventArgs e )
        {
            this.ToggleGrantView( true );
        }
        #endregion

        #region Methods
        protected override void UpdateView()
        {
            base.UpdateView();
            ( (Main)this.Master ).HideAddGrantAction();
        }
        #endregion

        #region Properties
        public override object Model
        {
            get
            {
                GrantVariable grantVariable = new GrantVariable();
                return grantVariable.Grant;
            }
        }
        #endregion

        #region Private Methods
        private void ToggleGrantView( bool editModel )
        {
            this.cmdEdit.Visible = !editModel;
            this.cmdSave.Visible = editModel;
            this.cmdCancel.Visible = editModel;
            this.ucGrantDetailView.Visible = !editModel;
            this.ucGrantEditView.Visible = editModel;
            this.TabContainer1.Visible = !editModel;
        }

        private List<Note> NotesFor( Grant grant )
        {
            ServiceMemberController controller = new ServiceMemberController();
            return controller.NotesFrom( grant.Case.ServiceMember );
        }

        protected void OnNoteAdded( Note note )
        {
            ServiceMemberController controller = new ServiceMemberController();
            controller.SaveNote( this.Grant.Case.ServiceMember, note );
            this.ucNoteListView.Model = this.NotesFor( this.Grant );
        }
        #endregion

        #region Private Properties
        private Grant Grant
        {
            get
            {
                return (Grant)this.Model;
            }
        }
        private string RedirectURL
        {
            get
            {
                if ( this.Session[REDIRECT_URL] != null )
                {
                    return this.Session[REDIRECT_URL].ToString();
                }
                return String.Empty;
            }
            set
            {
                this.Session[REDIRECT_URL] = value;
            }
        }
        #endregion

        #region Construction and Finalization
        #endregion

        #region Data Elements
        #endregion
    }
}

